from django.shortcuts import render
from rest_framework.views import APIView,Response
from rest_framework.viewsets import ViewSet
from utils.force_login import check_login
from orders.models import Order,OrderGoods,Dishes,Shops,Comment
from orders.serializer import OrderSer,CommentSer
from users.models import UserModel
from users.serializer import UserSer
from shops.serializer import ShopsModelSer

# 获取订单
class Order_get(APIView):
    def get(self,request,id):
        order = Order.objects.get(order_id=id)
        shopping = OrderGoods.objects.filter(order_id=id).all()
        order_list = []
        data_ = {
            'shop_id': order.shop_id,
            'order_id':order.order_id,
            'order_num': order.total_count,
            'order_price': order.total_amount,
        }
        alist = []
        for i in shopping:
            data = {
                'dishes_id': i.dishes.id,
                'name': i.dishes.dishes_name,
                'dishes_price': i.price,
                'count': i.count,
                'dishes_picture': i.dishes.dishes_picture
            }
            alist.append(data)
        data_['order_dishes'] = alist
        order_list.append(data_)
        return Response({'code':200,'msg':'获取订单成功','data':order_list})


# 用户发布评论
class User_comment(APIView):
    @check_login
    def post(self,request):
        content = request.data.get('content')
        order_shopping = request.data.get('order_shopping')
        comment_id = request.data.get('comment_id')
        user_id = request.user.id
        print(f'评论内容{content},订单信息{order_shopping},用户id{user_id},被评论的id{comment_id}')
        order_id = ''
        shop_id = ''
        for i in order_shopping:
            order_id = i['order_id']
            for j in i['order_dishes']:
                dishes = Dishes.objects.get(id=j['dishes_id'])
                shops = Shops.objects.get(id=dishes.shops_id)
                shopser = ShopsModelSer(shops)
                shop_id = shopser.data.get('id')

        if not comment_id:
            Comment.objects.create(user_id=user_id,shops_id=shop_id,order_id=order_id,content=content,is_reply=False)
            return Response({'code':200,'msg':'发布评论成功'})
        else:
            Comment.objects.create(user_id=user_id, shops_id=shop_id, content=content,
                                   is_reply=True,comment_id_id=comment_id)
            return Response({'code':200,'msg':'回复评论成功'})



class Get_user_comment(APIView):
    def get(self,request,id):
        order_id = id
        comment = Comment.objects.filter(order_id=order_id).all()
        order_list =[]
        for i in comment:
            user = UserModel.objects.get(id=i.user_id)
            data = {
                'nickname':user.nickname,
                'profile_photo':user.profile_photo,
                'content':i.content,
                'id':i.id

            }
            comment = Comment.objects.filter(comment_id=i.id).all()
            shops_list = []
            for j in comment:
                print('>>>>',j)
                shop = Shops.objects.get(id=j.shops_id)
                data_={
                    'shop_name':shop.shops_name,
                    'logo':shop.logo,
                    'content':j.content,
                    'id':j.id
                }
                shops_list.append(data_)
            data['shops_list'] = shops_list
            order_list.append(data)
        return Response({'code':200,'msg':'获取评论成功','data':order_list})



